﻿using System.Modules.SharePoint.Constants;
using System.Modules.SharePoint.Contracts.DTO;
using System.Reflection;
using Microsoft.SharePoint.Navigation;

namespace System.Modules.SharePoint.Extensions
{
    /// <summary>
    /// SP Navigation Node Extensions
    /// </summary>
    public static class SPNavigationNodeExtensions
    {
        #region Promote to Heading
        /// <summary>
        /// Promote to Heading
        /// </summary>
        /// <param name="node"></param>
        public static void PromoteToHeading(this SPNavigationNode node)
        {
            var navigationNode = new BaseSPNavigationNode {BlankUrl = true, LastModifiedDate = DateTime.Now, Target = string.Empty, vti_navsequencechild = true, UrlQueryString = string.Empty, CreatedDate = DateTime.Now, Description = node.Title, UrlFragment = string.Empty, NodeType = SPNavigationNodeType.HEADING, Audience = string.Empty};
            node.BindNavigationNode(navigationNode);
            node.Update();
        }
        #endregion

        #region Promote to Link
        /// <summary>
        /// Promote to Link
        /// </summary>
        /// <param name="node"></param>
        public static void PromoteToLink(this SPNavigationNode node)
        {
            var navigationNode = new BaseSPNavigationNode {BlankUrl = false, LastModifiedDate = DateTime.Now, Target = string.Empty, vti_navsequencechild = true, UrlQueryString = string.Empty, CreatedDate = DateTime.Now, Description = node.Title, UrlFragment = string.Empty, NodeType = SPNavigationNodeType.AUTHORED_LINK_PLAIN, Audience = string.Empty};
            node.BindNavigationNode(navigationNode);
            node.Update();
        }
        #endregion

        #region Bind Node
        /// <summary>
        /// This method binds an object of Type T into SP Navigation Node.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="node"></param>
        /// <param name="value"></param>
        public static void BindNavigationNode<T>(this SPNavigationNode node, T value)
        {
            Type type = typeof (T);
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                string propertyName = property.Name;
                if (node.Properties.ContainsKey(propertyName) && property.CanRead)
                {
                    node.Properties[propertyName] = property.GetValue(value, null);
                }
            }
        }

        /// <summary>
        /// This method binds an object of Type BaseSPNavigationNode into SP Navigation Node.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="value"></param>
        public static void BindNavigationNode(this SPNavigationNode node, BaseSPNavigationNode value)
        {
            Type type = typeof (BaseSPNavigationNode);
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                string propertyName = property.Name;
                if (node.Properties.ContainsKey(propertyName) && property.CanRead)
                {
                    node.Properties[propertyName] = property.GetValue(value, null);
                }
            }
        }
        #endregion
    }
}